<template>
  <!-- 添加或修改项目基础信息管理对话框 -->
  <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body v-dialogDrag width="90%">
    <el-row>
      <el-col :span="8">
        <el-card class="box-card">
          <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="请购单号" prop="nos">
              <el-input v-model="form.nos" placeholder="请输入请购单号" disabled/>
            </el-form-item>
            <el-form-item label="订单编号" prop="code">
              <el-input v-model="form.code" placeholder="请输入请购单号" disabled/>
            </el-form-item>
            <el-form-item label="客户名称" prop="customerName">
              <el-input v-model="form.customerName" placeholder="请输入请购单号" disabled/>
            </el-form-item>
            <el-form-item label="业务员" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入业务员" disabled/>
            </el-form-item>
            <el-form-item label="业务类型" prop="ywType">
              <el-select v-model="form.ywType" placeholder="请选择业务类型" clearable>
                <el-option v-for="dict in dict.type.yewu_type"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="采购类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择采购类型" clearable>
                <el-option v-for="dict in dict.type.caigou_type"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="请购日期" prop="ddDate">
              <el-date-picker clearable
                              v-model="form.ddDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择请购日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="需求日期" prop="subDate">
              <el-date-picker clearable
                              v-model="form.subDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择需求日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请选择备注"/>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
      <el-col :span="16">
        <el-card class="box-card">
          <el-form ref="formList" :model="formList" :rules="listRules">
            <el-table :data="columns" row-key="id" v-loading="loading" @selection-change="handleSelectionChange"
                      ref="table" @sort-change="handleSortChange" :max-height="maxHeight">
              <el-table-column sortable type="selection" width="55" align="center"/>
              <el-table-column sortable align="center" label="序号" type="index" width="50">
                <template slot-scope="scope">
                  <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column sortable label="产品编码" align="center" prop="goodsCode"/>
              <el-table-column sortable align="center" label="产品名称" prop="goodsName"/>
              <el-table-column sortable align="center" label="产品型号" prop="goodModel"/>
              <el-table-column sortable label="产品状态" align="center" prop="status">
                <template slot-scope="scope">
                  <dict-tag :options="dict.type.goods_status" :value="scope.row.status"/>
                </template>
              </el-table-column>
               <el-table-column sortable align="center" label="产品类型" prop="type"/>
              <el-table-column sortable align="center" label="需求日期" prop="deliveryTime"/>
              <el-table-column sortable align="center" label="订单数量" prop="orderCount"/>
              <el-table-column sortable label="是否自制" align="center" prop="isdo" width="100">
                <template slot-scope="scope">
                  <el-select v-model="scope.row.isdo" placeholder="是否自制" clearable>
                    <el-option v-for="dict in dict.type.isdo"
                               :key="dict.value" :label="dict.label" :value="dict.value"/>
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column sortable label="(请购/自制)数量" align="center" prop="pleaseBuyCount" width="210">
                <template slot-scope="scope">
                  <el-input-number size="mini" v-model="scope.row.pleaseBuyCount"></el-input-number>
                </template>
              </el-table-column>
              <el-table-column label="备注" align="center" prop="remark" width="210">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.remark"></el-input>
                </template>
              </el-table-column>
            </el-table>
          </el-form>

        </el-card>
      </el-col>
    </el-row>
    <div class="dialog-footer" slot="footer">
      <el-button type="primary" @click="selectContDo('1')">勾选自制</el-button>
      <el-button type="primary" @click="selectContDo('0')">勾选采购</el-button>
      <!--</div>
      <div class="dialog-footer" slot="footer">-->
      <el-button type="primary" @click="selfControlBnt(1)">全部自制</el-button>
      <el-button type="primary" @click="selfControlBnt(2)">全部采购</el-button>
      <el-button type="primary" @click="submitBuyForm">提交到采购</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>


<script>

import {addAction, deleteAction, getAction, getById} from '@/api/manage'
import Treeselect from "@riophae/vue-treeselect";

export default {
  name: "PleaseBuyFormGoods",
  dicts: ['isdo', 'yewu_type', 'caigou_type', 'goods_status'],
  components: {
    Treeselect
    /* GoodsOrderinChose,
     GoodSaleForm*/
  },
  data() {
    return {
      maxHeight: (window.innerHeight * 0.9 - 260),
      // 遮罩层
      loading: true,
      activeName: "first",
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "产品请购",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      formList: {},
      proId: "",
      status: "",
      columns: [],
      total: 0,
      // 表单校验
      rules: {
        ddDate: [
          {required: true, message: "请购日期不能为空", trigger: "blur"}
        ],
        subDate: [
          {required: true, message: "需求日期不能为空", trigger: "blur"}
        ],
      },
      listRules: {},
      queryParams: {
        pageNum: 1,
        pageSize: 1000,
        id: null,
        name: null,
        taxNo: null,
        addr: null,
        tel: null,
        bankName: null,
        bankNo: null,
        info: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        type: null,
        lvdoId: null,
        lvdoNo: null,
        mainId: null,
        isBuy: "1",
      },
      url: {
        list: '/goodsSaledo/goodsSaledo/list',
        pleaseBuyForPrj: '/goodsSaledo/goodsSaledo/pleaseBuyForPrj',
      },
    };
  },
  created() {

  },
  methods: {
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      /*for(var i = 0; i < selection.length; i++){
        this.columns.forEach(element => {
          if(element.id===selection.id){
            element.isdo = isdo;
          }
        });
      }*/
    },
    selfControlBnt(e) {
      if (e == 1) {
        this.columns.forEach(element => {
          element.isdo = '1'
        });
      } else {
        this.columns.forEach(element => {
          element.isdo = '0'
        });
      }
    },
    selectContDo(isdo) {
      /*this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length*/
      for (var i = 0; i < this.ids.length; i++) {
        this.columns.forEach(element => {
          if (element.id === this.ids[i]) {
            element.isdo = isdo;
          }
        });
      }
    },
    refreshPageData() {
      let that = this;
      let param = {
        id: that.proId
      }
      that.handleUpdate(param);
    },
    handleChose() {
      this.$refs.formChose.chose(this.proId, "2");
    },
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.columns = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        name: null,
        salesmanId: null,
        salesmanName: null,
        customId: null,
        customName: null,
        companyName: null,
        contractDate: null,
        contractMoney: null,
        auditMoney: null,
        checkDate: null,
        checkState: null,
        fundCallback: null,
        infomation: null,
      };
      this.resetForm("form");
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = row;
      this.queryParams.mainId = row.id;
      // this.queryParams.status=row.status;
      // this.queryParams.projectId = row.mainId;
      this.open = true;
      this.getList();
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    handleUpdateSale(row) {
      this.$refs.goodSaleForm.handleUpdateShow(row);
    },
    /** 提交按钮 */
    submitBuyForm() {
      let that = this;
      if (that.columns.length === 0) {
        that.$message({
          message: "请先添加商品",
          type: "warning"
        });
        return;
      }
      that.$refs["form"].validate(valid => {
        if (valid) {
          const params = Object.assign({});
          params.lvDo = that.form;
          params.mainId = that.queryParams.mainId;
          params.projectId = that.queryParams.projectId;
          params.proStatus = that.queryParams.status;
          params.columns = that._data.columns;
          addAction(this.url.pleaseBuyForPrj, params).then(response => {
            this.$modal.msgSuccess("请购成功");
            this.open = false;
            this.$emit('ok');
          });
        }
      });
    },
  }
};
</script>
<style scoped>

.box-card {
  margin: 5px;
}

</style>
